<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - Lua 5.1.5 - src/lbaselib.c</title>
  <link rel="stylesheet" type="text/css" href="../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../index.html">top level</a> - <a href="index.html">src</a> - lbaselib.c</td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">Lua 5.1.5</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">353</td>
            <td class="headerCovTableEntry">366</td>
            <td class="headerCovTableEntryHi">96.4 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2019-06-13 19:44:15</td>
            <td></td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<span class="lineNum">       1 </span>            : /*
<span class="lineNum">       2 </span>            : ** $Id: lbaselib.c,v 1.191.1.6 2008/02/14 16:46:22 roberto Exp $
<span class="lineNum">       3 </span>            : ** Basic library
<span class="lineNum">       4 </span>            : ** See Copyright Notice in lua.h
<span class="lineNum">       5 </span>            : */
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : 
<span class="lineNum">       9 </span>            : #include &lt;ctype.h&gt;
<span class="lineNum">      10 </span>            : #include &lt;stdio.h&gt;
<span class="lineNum">      11 </span>            : #include &lt;stdlib.h&gt;
<span class="lineNum">      12 </span>            : #include &lt;string.h&gt;
<span class="lineNum">      13 </span>            : 
<span class="lineNum">      14 </span>            : #define lbaselib_c
<span class="lineNum">      15 </span>            : #define LUA_LIB
<span class="lineNum">      16 </span>            : 
<span class="lineNum">      17 </span>            : #include &quot;lua.h&quot;
<span class="lineNum">      18 </span>            : 
<span class="lineNum">      19 </span>            : #include &quot;lauxlib.h&quot;
<span class="lineNum">      20 </span>            : #include &quot;lualib.h&quot;
<span class="lineNum">      21 </span>            : 
<span class="lineNum">      22 </span>            : 
<span class="lineNum">      23 </span>            : 
<span class="lineNum">      24 </span>            : 
<span class="lineNum">      25 </span>            : /*
<span class="lineNum">      26 </span>            : ** If your system does not support `stdout', you can just remove this function.
<span class="lineNum">      27 </span>            : ** If you need, you can define your own `print' function, following this
<span class="lineNum">      28 </span>            : ** model but changing `fputs' to put the strings at a proper place
<span class="lineNum">      29 </span>            : ** (a console window or a log file, for instance).
<span class="lineNum">      30 </span>            : */
<span class="lineNum">      31 </span><span class="lineCov">       1789 : static int luaB_print (lua_State *L) {</span>
<span class="lineNum">      32 </span><span class="lineCov">       1789 :   int n = lua_gettop(L);  /* number of arguments */</span>
<span class="lineNum">      33 </span>            :   int i;
<span class="lineNum">      34 </span><span class="lineCov">       1789 :   lua_getglobal(L, &quot;tostring&quot;);</span>
<span class="lineNum">      35 </span><span class="lineCov">       3580 :   for (i=1; i&lt;=n; i++) {</span>
<span class="lineNum">      36 </span>            :     const char *s;
<span class="lineNum">      37 </span><span class="lineCov">       1792 :     lua_pushvalue(L, -1);  /* function to be called */</span>
<span class="lineNum">      38 </span><span class="lineCov">       1792 :     lua_pushvalue(L, i);   /* value to print */</span>
<span class="lineNum">      39 </span><span class="lineCov">       1792 :     lua_call(L, 1, 1);</span>
<span class="lineNum">      40 </span><span class="lineCov">       1792 :     s = lua_tostring(L, -1);  /* get result */</span>
<span class="lineNum">      41 </span><span class="lineCov">       1792 :     if (s == NULL)</span>
<span class="lineNum">      42 </span><span class="lineCov">          1 :       return luaL_error(L, LUA_QL(&quot;tostring&quot;) &quot; must return a string to &quot;</span>
<span class="lineNum">      43 </span>            :                            LUA_QL(&quot;print&quot;));
<span class="lineNum">      44 </span><span class="lineCov">       1791 :     if (i&gt;1) fputs(&quot;\t&quot;, stdout);</span>
<span class="lineNum">      45 </span><span class="lineCov">       1791 :     fputs(s, stdout);</span>
<span class="lineNum">      46 </span><span class="lineCov">       1791 :     lua_pop(L, 1);  /* pop result */</span>
<span class="lineNum">      47 </span>            :   }
<span class="lineNum">      48 </span><span class="lineCov">       1788 :   fputs(&quot;\n&quot;, stdout);</span>
<span class="lineNum">      49 </span><span class="lineCov">       1788 :   return 0;</span>
<span class="lineNum">      50 </span>            : }
<span class="lineNum">      51 </span>            : 
<span class="lineNum">      52 </span>            : 
<span class="lineNum">      53 </span><span class="lineCov">        119 : static int luaB_tonumber (lua_State *L) {</span>
<span class="lineNum">      54 </span><span class="lineCov">        119 :   int base = luaL_optint(L, 2, 10);</span>
<span class="lineNum">      55 </span><span class="lineCov">        119 :   if (base == 10) {  /* standard conversion */</span>
<span class="lineNum">      56 </span><span class="lineCov">        115 :     luaL_checkany(L, 1);</span>
<span class="lineNum">      57 </span><span class="lineCov">        114 :     if (lua_isnumber(L, 1)) {</span>
<span class="lineNum">      58 </span><span class="lineCov">        111 :       lua_pushnumber(L, lua_tonumber(L, 1));</span>
<span class="lineNum">      59 </span><span class="lineCov">        111 :       return 1;</span>
<span class="lineNum">      60 </span>            :     }
<span class="lineNum">      61 </span>            :   }
<span class="lineNum">      62 </span>            :   else {
<span class="lineNum">      63 </span><span class="lineCov">          4 :     const char *s1 = luaL_checkstring(L, 1);</span>
<span class="lineNum">      64 </span>            :     char *s2;
<span class="lineNum">      65 </span>            :     unsigned long n;
<span class="lineNum">      66 </span><span class="lineCov">          4 :     luaL_argcheck(L, 2 &lt;= base &amp;&amp; base &lt;= 36, 2, &quot;base out of range&quot;);</span>
<span class="lineNum">      67 </span><span class="lineCov">          3 :     n = strtoul(s1, &amp;s2, base);</span>
<span class="lineNum">      68 </span><span class="lineCov">          3 :     if (s1 != s2) {  /* at least one valid digit? */</span>
<span class="lineNum">      69 </span><span class="lineCov">          5 :       while (isspace((unsigned char)(*s2))) s2++;  /* skip trailing spaces */</span>
<span class="lineNum">      70 </span><span class="lineCov">          3 :       if (*s2 == '\0') {  /* no invalid trailing characters? */</span>
<span class="lineNum">      71 </span><span class="lineCov">          3 :         lua_pushnumber(L, (lua_Number)n);</span>
<span class="lineNum">      72 </span><span class="lineCov">          3 :         return 1;</span>
<span class="lineNum">      73 </span>            :       }
<span class="lineNum">      74 </span>            :     }
<span class="lineNum">      75 </span>            :   }
<span class="lineNum">      76 </span><span class="lineCov">          3 :   lua_pushnil(L);  /* else not a number */</span>
<span class="lineNum">      77 </span><span class="lineCov">          3 :   return 1;</span>
<span class="lineNum">      78 </span>            : }
<span class="lineNum">      79 </span>            : 
<span class="lineNum">      80 </span>            : 
<span class="lineNum">      81 </span><span class="lineCov">         12 : static int luaB_error (lua_State *L) {</span>
<span class="lineNum">      82 </span><span class="lineCov">         12 :   int level = luaL_optint(L, 2, 1);</span>
<span class="lineNum">      83 </span><span class="lineCov">         12 :   lua_settop(L, 1);</span>
<span class="lineNum">      84 </span><span class="lineCov">         12 :   if (lua_isstring(L, 1) &amp;&amp; level &gt; 0) {  /* add extra information? */</span>
<span class="lineNum">      85 </span><span class="lineCov">          7 :     luaL_where(L, level);</span>
<span class="lineNum">      86 </span><span class="lineCov">          7 :     lua_pushvalue(L, 1);</span>
<span class="lineNum">      87 </span><span class="lineCov">          7 :     lua_concat(L, 2);</span>
<span class="lineNum">      88 </span>            :   }
<span class="lineNum">      89 </span><span class="lineCov">         12 :   return lua_error(L);</span>
<span class="lineNum">      90 </span>            : }
<span class="lineNum">      91 </span>            : 
<span class="lineNum">      92 </span>            : 
<span class="lineNum">      93 </span><span class="lineCov">         14 : static int luaB_getmetatable (lua_State *L) {</span>
<span class="lineNum">      94 </span><span class="lineCov">         14 :   luaL_checkany(L, 1);</span>
<span class="lineNum">      95 </span><span class="lineCov">         14 :   if (!lua_getmetatable(L, 1)) {</span>
<span class="lineNum">      96 </span><span class="lineCov">          6 :     lua_pushnil(L);</span>
<span class="lineNum">      97 </span><span class="lineCov">          6 :     return 1;  /* no metatable */</span>
<span class="lineNum">      98 </span>            :   }
<span class="lineNum">      99 </span><span class="lineCov">          8 :   luaL_getmetafield(L, 1, &quot;__metatable&quot;);</span>
<span class="lineNum">     100 </span><span class="lineCov">          8 :   return 1;  /* returns either __metatable field (if present) or metatable */</span>
<span class="lineNum">     101 </span>            : }
<span class="lineNum">     102 </span>            : 
<span class="lineNum">     103 </span>            : 
<span class="lineNum">     104 </span><span class="lineCov">         85 : static int luaB_setmetatable (lua_State *L) {</span>
<span class="lineNum">     105 </span><span class="lineCov">         85 :   int t = lua_type(L, 2);</span>
<span class="lineNum">     106 </span><span class="lineCov">         85 :   luaL_checktype(L, 1, LUA_TTABLE);</span>
<span class="lineNum">     107 </span><span class="lineCov">         85 :   luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,</span>
<span class="lineNum">     108 </span>            :                     &quot;nil or table expected&quot;);
<span class="lineNum">     109 </span><span class="lineCov">         84 :   if (luaL_getmetafield(L, 1, &quot;__metatable&quot;))</span>
<span class="lineNum">     110 </span><span class="lineCov">          1 :     luaL_error(L, &quot;cannot change a protected metatable&quot;);</span>
<span class="lineNum">     111 </span><span class="lineCov">         83 :   lua_settop(L, 2);</span>
<span class="lineNum">     112 </span><span class="lineCov">         83 :   lua_setmetatable(L, 1);</span>
<span class="lineNum">     113 </span><span class="lineCov">         83 :   return 1;</span>
<span class="lineNum">     114 </span>            : }
<span class="lineNum">     115 </span>            : 
<span class="lineNum">     116 </span>            : 
<span class="lineNum">     117 </span><span class="lineCov">         27 : static void getfunc (lua_State *L, int opt) {</span>
<span class="lineNum">     118 </span><span class="lineCov">         27 :   if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);</span>
<span class="lineNum">     119 </span>            :   else {
<span class="lineNum">     120 </span>            :     lua_Debug ar;
<span class="lineNum">     121 </span><span class="lineCov">         18 :     int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1);</span>
<span class="lineNum">     122 </span><span class="lineCov">         17 :     luaL_argcheck(L, level &gt;= 0, 1, &quot;level must be non-negative&quot;);</span>
<span class="lineNum">     123 </span><span class="lineCov">         15 :     if (lua_getstack(L, level, &amp;ar) == 0)</span>
<span class="lineNum">     124 </span><span class="lineCov">          2 :       luaL_argerror(L, 1, &quot;invalid level&quot;);</span>
<span class="lineNum">     125 </span><span class="lineCov">         13 :     lua_getinfo(L, &quot;f&quot;, &amp;ar);</span>
<span class="lineNum">     126 </span><span class="lineCov">         13 :     if (lua_isnil(L, -1))</span>
<span class="lineNum">     127 </span><span class="lineNoCov">          0 :       luaL_error(L, &quot;no function environment for tail call at level %d&quot;,</span>
<span class="lineNum">     128 </span>            :                     level);
<span class="lineNum">     129 </span>            :   }
<span class="lineNum">     130 </span><span class="lineCov">         22 : }</span>
<span class="lineNum">     131 </span>            : 
<span class="lineNum">     132 </span>            : 
<span class="lineNum">     133 </span><span class="lineCov">         15 : static int luaB_getfenv (lua_State *L) {</span>
<span class="lineNum">     134 </span><span class="lineCov">         15 :   getfunc(L, 1);</span>
<span class="lineNum">     135 </span><span class="lineCov">         13 :   if (lua_iscfunction(L, -1))  /* is a C function? */</span>
<span class="lineNum">     136 </span><span class="lineCov">          4 :     lua_pushvalue(L, LUA_GLOBALSINDEX);  /* return the thread's global env. */</span>
<span class="lineNum">     137 </span>            :   else
<span class="lineNum">     138 </span><span class="lineCov">          9 :     lua_getfenv(L, -1);</span>
<span class="lineNum">     139 </span><span class="lineCov">         13 :   return 1;</span>
<span class="lineNum">     140 </span>            : }
<span class="lineNum">     141 </span>            : 
<span class="lineNum">     142 </span>            : 
<span class="lineNum">     143 </span><span class="lineCov">         12 : static int luaB_setfenv (lua_State *L) {</span>
<span class="lineNum">     144 </span><span class="lineCov">         12 :   luaL_checktype(L, 2, LUA_TTABLE);</span>
<span class="lineNum">     145 </span><span class="lineCov">         12 :   getfunc(L, 0);</span>
<span class="lineNum">     146 </span><span class="lineCov">          9 :   lua_pushvalue(L, 2);</span>
<span class="lineNum">     147 </span><span class="lineCov">          9 :   if (lua_isnumber(L, 1) &amp;&amp; lua_tonumber(L, 1) == 0) {</span>
<span class="lineNum">     148 </span>            :     /* change environment of current thread */
<span class="lineNum">     149 </span><span class="lineCov">          1 :     lua_pushthread(L);</span>
<span class="lineNum">     150 </span><span class="lineCov">          1 :     lua_insert(L, -2);</span>
<span class="lineNum">     151 </span><span class="lineCov">          1 :     lua_setfenv(L, -2);</span>
<span class="lineNum">     152 </span><span class="lineCov">          1 :     return 0;</span>
<span class="lineNum">     153 </span>            :   }
<span class="lineNum">     154 </span><span class="lineCov">          8 :   else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)</span>
<span class="lineNum">     155 </span><span class="lineCov">          1 :     luaL_error(L,</span>
<span class="lineNum">     156 </span>            :           LUA_QL(&quot;setfenv&quot;) &quot; cannot change environment of given object&quot;);
<span class="lineNum">     157 </span><span class="lineCov">          7 :   return 1;</span>
<span class="lineNum">     158 </span>            : }
<span class="lineNum">     159 </span>            : 
<span class="lineNum">     160 </span>            : 
<span class="lineNum">     161 </span><span class="lineCov">         17 : static int luaB_rawequal (lua_State *L) {</span>
<span class="lineNum">     162 </span><span class="lineCov">         17 :   luaL_checkany(L, 1);</span>
<span class="lineNum">     163 </span><span class="lineCov">         17 :   luaL_checkany(L, 2);</span>
<span class="lineNum">     164 </span><span class="lineCov">         17 :   lua_pushboolean(L, lua_rawequal(L, 1, 2));</span>
<span class="lineNum">     165 </span><span class="lineCov">         17 :   return 1;</span>
<span class="lineNum">     166 </span>            : }
<span class="lineNum">     167 </span>            : 
<span class="lineNum">     168 </span>            : 
<span class="lineNum">     169 </span><span class="lineCov">          5 : static int luaB_rawget (lua_State *L) {</span>
<span class="lineNum">     170 </span><span class="lineCov">          5 :   luaL_checktype(L, 1, LUA_TTABLE);</span>
<span class="lineNum">     171 </span><span class="lineCov">          5 :   luaL_checkany(L, 2);</span>
<span class="lineNum">     172 </span><span class="lineCov">          5 :   lua_settop(L, 2);</span>
<span class="lineNum">     173 </span><span class="lineCov">          5 :   lua_rawget(L, 1);</span>
<span class="lineNum">     174 </span><span class="lineCov">          5 :   return 1;</span>
<span class="lineNum">     175 </span>            : }
<span class="lineNum">     176 </span>            : 
<span class="lineNum">     177 </span><span class="lineCov">          3 : static int luaB_rawset (lua_State *L) {</span>
<span class="lineNum">     178 </span><span class="lineCov">          3 :   luaL_checktype(L, 1, LUA_TTABLE);</span>
<span class="lineNum">     179 </span><span class="lineCov">          3 :   luaL_checkany(L, 2);</span>
<span class="lineNum">     180 </span><span class="lineCov">          3 :   luaL_checkany(L, 3);</span>
<span class="lineNum">     181 </span><span class="lineCov">          3 :   lua_settop(L, 3);</span>
<span class="lineNum">     182 </span><span class="lineCov">          3 :   lua_rawset(L, 1);</span>
<span class="lineNum">     183 </span><span class="lineCov">          2 :   return 1;</span>
<span class="lineNum">     184 </span>            : }
<span class="lineNum">     185 </span>            : 
<span class="lineNum">     186 </span>            : 
<span class="lineNum">     187 </span><span class="lineCov">          1 : static int luaB_gcinfo (lua_State *L) {</span>
<span class="lineNum">     188 </span><span class="lineCov">          1 :   lua_pushinteger(L, lua_getgccount(L));</span>
<span class="lineNum">     189 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">     190 </span>            : }
<span class="lineNum">     191 </span>            : 
<span class="lineNum">     192 </span>            : 
<span class="lineNum">     193 </span><span class="lineCov">          7 : static int luaB_collectgarbage (lua_State *L) {</span>
<span class="lineNum">     194 </span>            :   static const char *const opts[] = {&quot;stop&quot;, &quot;restart&quot;, &quot;collect&quot;,
<span class="lineNum">     195 </span>            :     &quot;count&quot;, &quot;step&quot;, &quot;setpause&quot;, &quot;setstepmul&quot;, NULL};
<span class="lineNum">     196 </span>            :   static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
<span class="lineNum">     197 </span>            :     LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL};
<span class="lineNum">     198 </span><span class="lineCov">          7 :   int o = luaL_checkoption(L, 1, &quot;collect&quot;, opts);</span>
<span class="lineNum">     199 </span><span class="lineCov">          6 :   int ex = luaL_optint(L, 2, 0);</span>
<span class="lineNum">     200 </span><span class="lineCov">          6 :   int res = lua_gc(L, optsnum[o], ex);</span>
<span class="lineNum">     201 </span><span class="lineCov">          6 :   switch (optsnum[o]) {</span>
<span class="lineNum">     202 </span><span class="lineCov">          1 :     case LUA_GCCOUNT: {</span>
<span class="lineNum">     203 </span><span class="lineCov">          1 :       int b = lua_gc(L, LUA_GCCOUNTB, 0);</span>
<span class="lineNum">     204 </span><span class="lineCov">          1 :       lua_pushnumber(L, res + ((lua_Number)b/1024));</span>
<span class="lineNum">     205 </span><span class="lineCov">          1 :       return 1;</span>
<span class="lineNum">     206 </span>            :     }
<span class="lineNum">     207 </span><span class="lineCov">          1 :     case LUA_GCSTEP: {</span>
<span class="lineNum">     208 </span><span class="lineCov">          1 :       lua_pushboolean(L, res);</span>
<span class="lineNum">     209 </span><span class="lineCov">          1 :       return 1;</span>
<span class="lineNum">     210 </span>            :     }
<span class="lineNum">     211 </span><span class="lineCov">          4 :     default: {</span>
<span class="lineNum">     212 </span><span class="lineCov">          4 :       lua_pushnumber(L, res);</span>
<span class="lineNum">     213 </span><span class="lineCov">          4 :       return 1;</span>
<span class="lineNum">     214 </span>            :     }
<span class="lineNum">     215 </span>            :   }
<span class="lineNum">     216 </span>            : }
<span class="lineNum">     217 </span>            : 
<span class="lineNum">     218 </span>            : 
<span class="lineNum">     219 </span><span class="lineCov">        129 : static int luaB_type (lua_State *L) {</span>
<span class="lineNum">     220 </span><span class="lineCov">        129 :   luaL_checkany(L, 1);</span>
<span class="lineNum">     221 </span><span class="lineCov">        128 :   lua_pushstring(L, luaL_typename(L, 1));</span>
<span class="lineNum">     222 </span><span class="lineCov">        128 :   return 1;</span>
<span class="lineNum">     223 </span>            : }
<span class="lineNum">     224 </span>            : 
<span class="lineNum">     225 </span>            : 
<span class="lineNum">     226 </span><span class="lineCov">         41 : static int luaB_next (lua_State *L) {</span>
<span class="lineNum">     227 </span><span class="lineCov">         41 :   luaL_checktype(L, 1, LUA_TTABLE);</span>
<span class="lineNum">     228 </span><span class="lineCov">         40 :   lua_settop(L, 2);  /* create a 2nd argument if there isn't one */</span>
<span class="lineNum">     229 </span><span class="lineCov">         40 :   if (lua_next(L, 1))</span>
<span class="lineNum">     230 </span><span class="lineCov">         28 :     return 2;</span>
<span class="lineNum">     231 </span>            :   else {
<span class="lineNum">     232 </span><span class="lineCov">         11 :     lua_pushnil(L);</span>
<span class="lineNum">     233 </span><span class="lineCov">         11 :     return 1;</span>
<span class="lineNum">     234 </span>            :   }
<span class="lineNum">     235 </span>            : }
<span class="lineNum">     236 </span>            : 
<span class="lineNum">     237 </span>            : 
<span class="lineNum">     238 </span><span class="lineCov">          6 : static int luaB_pairs (lua_State *L) {</span>
<span class="lineNum">     239 </span><span class="lineCov">          6 :   luaL_checktype(L, 1, LUA_TTABLE);</span>
<span class="lineNum">     240 </span><span class="lineCov">          6 :   lua_pushvalue(L, lua_upvalueindex(1));  /* return generator, */</span>
<span class="lineNum">     241 </span><span class="lineCov">          6 :   lua_pushvalue(L, 1);  /* state, */</span>
<span class="lineNum">     242 </span><span class="lineCov">          6 :   lua_pushnil(L);  /* and initial value */</span>
<span class="lineNum">     243 </span><span class="lineCov">          6 :   return 3;</span>
<span class="lineNum">     244 </span>            : }
<span class="lineNum">     245 </span>            : 
<span class="lineNum">     246 </span>            : 
<span class="lineNum">     247 </span><span class="lineCov">      46283 : static int ipairsaux (lua_State *L) {</span>
<span class="lineNum">     248 </span><span class="lineCov">      46283 :   int i = luaL_checkint(L, 2);</span>
<span class="lineNum">     249 </span><span class="lineCov">      46283 :   luaL_checktype(L, 1, LUA_TTABLE);</span>
<span class="lineNum">     250 </span><span class="lineCov">      46283 :   i++;  /* next value */</span>
<span class="lineNum">     251 </span><span class="lineCov">      46283 :   lua_pushinteger(L, i);</span>
<span class="lineNum">     252 </span><span class="lineCov">      46283 :   lua_rawgeti(L, 1, i);</span>
<span class="lineNum">     253 </span><span class="lineCov">      46283 :   return (lua_isnil(L, -1)) ? 0 : 2;</span>
<span class="lineNum">     254 </span>            : }
<span class="lineNum">     255 </span>            : 
<span class="lineNum">     256 </span>            : 
<span class="lineNum">     257 </span><span class="lineCov">       5925 : static int luaB_ipairs (lua_State *L) {</span>
<span class="lineNum">     258 </span><span class="lineCov">       5925 :   luaL_checktype(L, 1, LUA_TTABLE);</span>
<span class="lineNum">     259 </span><span class="lineCov">       5925 :   lua_pushvalue(L, lua_upvalueindex(1));  /* return generator, */</span>
<span class="lineNum">     260 </span><span class="lineCov">       5925 :   lua_pushvalue(L, 1);  /* state, */</span>
<span class="lineNum">     261 </span><span class="lineCov">       5925 :   lua_pushinteger(L, 0);  /* and initial value */</span>
<span class="lineNum">     262 </span><span class="lineCov">       5925 :   return 3;</span>
<span class="lineNum">     263 </span>            : }
<span class="lineNum">     264 </span>            : 
<span class="lineNum">     265 </span>            : 
<span class="lineNum">     266 </span><span class="lineCov">        206 : static int load_aux (lua_State *L, int status) {</span>
<span class="lineNum">     267 </span><span class="lineCov">        206 :   if (status == 0)  /* OK? */</span>
<span class="lineNum">     268 </span><span class="lineCov">        171 :     return 1;</span>
<span class="lineNum">     269 </span>            :   else {
<span class="lineNum">     270 </span><span class="lineCov">         35 :     lua_pushnil(L);</span>
<span class="lineNum">     271 </span><span class="lineCov">         35 :     lua_insert(L, -2);  /* put before error message */</span>
<span class="lineNum">     272 </span><span class="lineCov">         35 :     return 2;  /* return nil plus error message */</span>
<span class="lineNum">     273 </span>            :   }
<span class="lineNum">     274 </span>            : }
<span class="lineNum">     275 </span>            : 
<span class="lineNum">     276 </span>            : 
<span class="lineNum">     277 </span><span class="lineCov">        197 : static int luaB_loadstring (lua_State *L) {</span>
<span class="lineNum">     278 </span>            :   size_t l;
<span class="lineNum">     279 </span><span class="lineCov">        197 :   const char *s = luaL_checklstring(L, 1, &amp;l);</span>
<span class="lineNum">     280 </span><span class="lineCov">        197 :   const char *chunkname = luaL_optstring(L, 2, s);</span>
<span class="lineNum">     281 </span><span class="lineCov">        197 :   return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));</span>
<span class="lineNum">     282 </span>            : }
<span class="lineNum">     283 </span>            : 
<span class="lineNum">     284 </span>            : 
<span class="lineNum">     285 </span><span class="lineCov">          4 : static int luaB_loadfile (lua_State *L) {</span>
<span class="lineNum">     286 </span><span class="lineCov">          4 :   const char *fname = luaL_optstring(L, 1, NULL);</span>
<span class="lineNum">     287 </span><span class="lineCov">          4 :   return load_aux(L, luaL_loadfile(L, fname));</span>
<span class="lineNum">     288 </span>            : }
<span class="lineNum">     289 </span>            : 
<span class="lineNum">     290 </span>            : 
<span class="lineNum">     291 </span>            : /*
<span class="lineNum">     292 </span>            : ** Reader for generic `load' function: `lua_load' uses the
<span class="lineNum">     293 </span>            : ** stack for internal stuff, so the reader cannot change the
<span class="lineNum">     294 </span>            : ** stack top. Instead, it keeps its resulting string in a
<span class="lineNum">     295 </span>            : ** reserved slot inside the stack.
<span class="lineNum">     296 </span>            : */
<span class="lineNum">     297 </span><span class="lineCov">          9 : static const char *generic_reader (lua_State *L, void *ud, size_t *size) {</span>
<span class="lineNum">     298 </span>            :   (void)ud;  /* to avoid warnings */
<span class="lineNum">     299 </span><span class="lineCov">          9 :   luaL_checkstack(L, 2, &quot;too many nested functions&quot;);</span>
<span class="lineNum">     300 </span><span class="lineCov">          9 :   lua_pushvalue(L, 1);  /* get function */</span>
<span class="lineNum">     301 </span><span class="lineCov">          9 :   lua_call(L, 0, 1);  /* call it */</span>
<span class="lineNum">     302 </span><span class="lineCov">          9 :   if (lua_isnil(L, -1)) {</span>
<span class="lineNum">     303 </span><span class="lineCov">          5 :     *size = 0;</span>
<span class="lineNum">     304 </span><span class="lineCov">          5 :     return NULL;</span>
<span class="lineNum">     305 </span>            :   }
<span class="lineNum">     306 </span><span class="lineCov">          4 :   else if (lua_isstring(L, -1)) {</span>
<span class="lineNum">     307 </span><span class="lineCov">          3 :     lua_replace(L, 3);  /* save string in a reserved stack slot */</span>
<span class="lineNum">     308 </span><span class="lineCov">          3 :     return lua_tolstring(L, 3, size);</span>
<span class="lineNum">     309 </span>            :   }
<span class="lineNum">     310 </span><span class="lineCov">          1 :   else luaL_error(L, &quot;reader function must return a string&quot;);</span>
<span class="lineNum">     311 </span><span class="lineNoCov">          0 :   return NULL;  /* to avoid warnings */</span>
<span class="lineNum">     312 </span>            : }
<span class="lineNum">     313 </span>            : 
<span class="lineNum">     314 </span>            : 
<span class="lineNum">     315 </span><span class="lineCov">          5 : static int luaB_load (lua_State *L) {</span>
<span class="lineNum">     316 </span>            :   int status;
<span class="lineNum">     317 </span><span class="lineCov">          5 :   const char *cname = luaL_optstring(L, 2, &quot;=(load)&quot;);</span>
<span class="lineNum">     318 </span><span class="lineCov">          5 :   luaL_checktype(L, 1, LUA_TFUNCTION);</span>
<span class="lineNum">     319 </span><span class="lineCov">          5 :   lua_settop(L, 3);  /* function, eventual name, plus one reserved slot */</span>
<span class="lineNum">     320 </span><span class="lineCov">          5 :   status = lua_load(L, generic_reader, NULL, cname);</span>
<span class="lineNum">     321 </span><span class="lineCov">          5 :   return load_aux(L, status);</span>
<span class="lineNum">     322 </span>            : }
<span class="lineNum">     323 </span>            : 
<span class="lineNum">     324 </span>            : 
<span class="lineNum">     325 </span><span class="lineCov">          4 : static int luaB_dofile (lua_State *L) {</span>
<span class="lineNum">     326 </span><span class="lineCov">          4 :   const char *fname = luaL_optstring(L, 1, NULL);</span>
<span class="lineNum">     327 </span><span class="lineCov">          4 :   int n = lua_gettop(L);</span>
<span class="lineNum">     328 </span><span class="lineCov">          4 :   if (luaL_loadfile(L, fname) != 0) lua_error(L);</span>
<span class="lineNum">     329 </span><span class="lineCov">          2 :   lua_call(L, 0, LUA_MULTRET);</span>
<span class="lineNum">     330 </span><span class="lineCov">          2 :   return lua_gettop(L) - n;</span>
<span class="lineNum">     331 </span>            : }
<span class="lineNum">     332 </span>            : 
<span class="lineNum">     333 </span>            : 
<span class="lineNum">     334 </span><span class="lineCov">       5926 : static int luaB_assert (lua_State *L) {</span>
<span class="lineNum">     335 </span><span class="lineCov">       5926 :   luaL_checkany(L, 1);</span>
<span class="lineNum">     336 </span><span class="lineCov">       5924 :   if (!lua_toboolean(L, 1))</span>
<span class="lineNum">     337 </span><span class="lineCov">          6 :     return luaL_error(L, &quot;%s&quot;, luaL_optstring(L, 2, &quot;assertion failed!&quot;));</span>
<span class="lineNum">     338 </span><span class="lineCov">       5918 :   return lua_gettop(L);</span>
<span class="lineNum">     339 </span>            : }
<span class="lineNum">     340 </span>            : 
<span class="lineNum">     341 </span>            : 
<span class="lineNum">     342 </span><span class="lineCov">          6 : static int luaB_unpack (lua_State *L) {</span>
<span class="lineNum">     343 </span>            :   int i, e, n;
<span class="lineNum">     344 </span><span class="lineCov">          6 :   luaL_checktype(L, 1, LUA_TTABLE);</span>
<span class="lineNum">     345 </span><span class="lineCov">          6 :   i = luaL_optint(L, 2, 1);</span>
<span class="lineNum">     346 </span><span class="lineCov">          6 :   e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));</span>
<span class="lineNum">     347 </span><span class="lineCov">          6 :   if (i &gt; e) return 0;  /* empty range */</span>
<span class="lineNum">     348 </span><span class="lineCov">          5 :   n = e - i + 1;  /* number of elements */</span>
<span class="lineNum">     349 </span><span class="lineCov">          5 :   if (n &lt;= 0 || !lua_checkstack(L, n))  /* n &lt;= 0 means arith. overflow */</span>
<span class="lineNum">     350 </span><span class="lineNoCov">          0 :     return luaL_error(L, &quot;too many results to unpack&quot;);</span>
<span class="lineNum">     351 </span><span class="lineCov">          5 :   lua_rawgeti(L, 1, i);  /* push arg[i] (avoiding overflow problems) */</span>
<span class="lineNum">     352 </span><span class="lineCov">         13 :   while (i++ &lt; e)  /* push arg[i + 1...e] */</span>
<span class="lineNum">     353 </span><span class="lineCov">          8 :     lua_rawgeti(L, 1, i);</span>
<span class="lineNum">     354 </span><span class="lineCov">          5 :   return n;</span>
<span class="lineNum">     355 </span>            : }
<span class="lineNum">     356 </span>            : 
<span class="lineNum">     357 </span>            : 
<span class="lineNum">     358 </span><span class="lineCov">         12 : static int luaB_select (lua_State *L) {</span>
<span class="lineNum">     359 </span><span class="lineCov">         12 :   int n = lua_gettop(L);</span>
<span class="lineNum">     360 </span><span class="lineCov">         12 :   if (lua_type(L, 1) == LUA_TSTRING &amp;&amp; *lua_tostring(L, 1) == '#') {</span>
<span class="lineNum">     361 </span><span class="lineCov">          2 :     lua_pushinteger(L, n-1);</span>
<span class="lineNum">     362 </span><span class="lineCov">          2 :     return 1;</span>
<span class="lineNum">     363 </span>            :   }
<span class="lineNum">     364 </span>            :   else {
<span class="lineNum">     365 </span><span class="lineCov">         10 :     int i = luaL_checkint(L, 1);</span>
<span class="lineNum">     366 </span><span class="lineCov">         10 :     if (i &lt; 0) i = n + i;</span>
<span class="lineNum">     367 </span><span class="lineCov">          6 :     else if (i &gt; n) i = n;</span>
<span class="lineNum">     368 </span><span class="lineCov">         10 :     luaL_argcheck(L, 1 &lt;= i, 1, &quot;index out of range&quot;);</span>
<span class="lineNum">     369 </span><span class="lineCov">          8 :     return n - i;</span>
<span class="lineNum">     370 </span>            :   }
<span class="lineNum">     371 </span>            : }
<span class="lineNum">     372 </span>            : 
<span class="lineNum">     373 </span>            : 
<span class="lineNum">     374 </span><span class="lineCov">        499 : static int luaB_pcall (lua_State *L) {</span>
<span class="lineNum">     375 </span>            :   int status;
<span class="lineNum">     376 </span><span class="lineCov">        499 :   luaL_checkany(L, 1);</span>
<span class="lineNum">     377 </span><span class="lineCov">        499 :   status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);</span>
<span class="lineNum">     378 </span><span class="lineCov">        499 :   lua_pushboolean(L, (status == 0));</span>
<span class="lineNum">     379 </span><span class="lineCov">        499 :   lua_insert(L, 1);</span>
<span class="lineNum">     380 </span><span class="lineCov">        499 :   return lua_gettop(L);  /* return status + all results */</span>
<span class="lineNum">     381 </span>            : }
<span class="lineNum">     382 </span>            : 
<span class="lineNum">     383 </span>            : 
<span class="lineNum">     384 </span><span class="lineCov">          5 : static int luaB_xpcall (lua_State *L) {</span>
<span class="lineNum">     385 </span>            :   int status;
<span class="lineNum">     386 </span><span class="lineCov">          5 :   luaL_checkany(L, 2);</span>
<span class="lineNum">     387 </span><span class="lineCov">          4 :   lua_settop(L, 2);</span>
<span class="lineNum">     388 </span><span class="lineCov">          4 :   lua_insert(L, 1);  /* put error function under function to be called */</span>
<span class="lineNum">     389 </span><span class="lineCov">          4 :   status = lua_pcall(L, 0, LUA_MULTRET, 1);</span>
<span class="lineNum">     390 </span><span class="lineCov">          4 :   lua_pushboolean(L, (status == 0));</span>
<span class="lineNum">     391 </span><span class="lineCov">          4 :   lua_replace(L, 1);</span>
<span class="lineNum">     392 </span><span class="lineCov">          4 :   return lua_gettop(L);  /* return status + all results */</span>
<span class="lineNum">     393 </span>            : }
<span class="lineNum">     394 </span>            : 
<span class="lineNum">     395 </span>            : 
<span class="lineNum">     396 </span><span class="lineCov">       4074 : static int luaB_tostring (lua_State *L) {</span>
<span class="lineNum">     397 </span><span class="lineCov">       4074 :   luaL_checkany(L, 1);</span>
<span class="lineNum">     398 </span><span class="lineCov">       4073 :   if (luaL_callmeta(L, 1, &quot;__tostring&quot;))  /* is there a metafield? */</span>
<span class="lineNum">     399 </span><span class="lineCov">         40 :     return 1;  /* use its value */</span>
<span class="lineNum">     400 </span><span class="lineCov">       4032 :   switch (lua_type(L, 1)) {</span>
<span class="lineNum">     401 </span><span class="lineCov">       1750 :     case LUA_TNUMBER:</span>
<span class="lineNum">     402 </span><span class="lineCov">       1750 :       lua_pushstring(L, lua_tostring(L, 1));</span>
<span class="lineNum">     403 </span><span class="lineCov">       1750 :       break;</span>
<span class="lineNum">     404 </span><span class="lineCov">       2268 :     case LUA_TSTRING:</span>
<span class="lineNum">     405 </span><span class="lineCov">       2268 :       lua_pushvalue(L, 1);</span>
<span class="lineNum">     406 </span><span class="lineCov">       2268 :       break;</span>
<span class="lineNum">     407 </span><span class="lineCov">          3 :     case LUA_TBOOLEAN:</span>
<span class="lineNum">     408 </span><span class="lineCov">          3 :       lua_pushstring(L, (lua_toboolean(L, 1) ? &quot;true&quot; : &quot;false&quot;));</span>
<span class="lineNum">     409 </span><span class="lineCov">          3 :       break;</span>
<span class="lineNum">     410 </span><span class="lineCov">          5 :     case LUA_TNIL:</span>
<span class="lineNum">     411 </span><span class="lineCov">          5 :       lua_pushliteral(L, &quot;nil&quot;);</span>
<span class="lineNum">     412 </span><span class="lineCov">          5 :       break;</span>
<span class="lineNum">     413 </span><span class="lineCov">          6 :     default:</span>
<span class="lineNum">     414 </span><span class="lineCov">          6 :       lua_pushfstring(L, &quot;%s: %p&quot;, luaL_typename(L, 1), lua_topointer(L, 1));</span>
<span class="lineNum">     415 </span><span class="lineCov">          6 :       break;</span>
<span class="lineNum">     416 </span>            :   }
<span class="lineNum">     417 </span><span class="lineCov">       4032 :   return 1;</span>
<span class="lineNum">     418 </span>            : }
<span class="lineNum">     419 </span>            : 
<span class="lineNum">     420 </span>            : 
<span class="lineNum">     421 </span><span class="lineCov">          4 : static int luaB_newproxy (lua_State *L) {</span>
<span class="lineNum">     422 </span><span class="lineCov">          4 :   lua_settop(L, 1);</span>
<span class="lineNum">     423 </span><span class="lineCov">          4 :   lua_newuserdata(L, 0);  /* create proxy */</span>
<span class="lineNum">     424 </span><span class="lineCov">          4 :   if (lua_toboolean(L, 1) == 0)</span>
<span class="lineNum">     425 </span><span class="lineCov">          1 :     return 1;  /* no metatable */</span>
<span class="lineNum">     426 </span><span class="lineCov">          3 :   else if (lua_isboolean(L, 1)) {</span>
<span class="lineNum">     427 </span><span class="lineCov">          1 :     lua_newtable(L);  /* create a new metatable `m' ... */</span>
<span class="lineNum">     428 </span><span class="lineCov">          1 :     lua_pushvalue(L, -1);  /* ... and mark `m' as a valid metatable */</span>
<span class="lineNum">     429 </span><span class="lineCov">          1 :     lua_pushboolean(L, 1);</span>
<span class="lineNum">     430 </span><span class="lineCov">          1 :     lua_rawset(L, lua_upvalueindex(1));  /* weaktable[m] = true */</span>
<span class="lineNum">     431 </span>            :   }
<span class="lineNum">     432 </span>            :   else {
<span class="lineNum">     433 </span><span class="lineCov">          2 :     int validproxy = 0;  /* to check if weaktable[metatable(u)] == true */</span>
<span class="lineNum">     434 </span><span class="lineCov">          2 :     if (lua_getmetatable(L, 1)) {</span>
<span class="lineNum">     435 </span><span class="lineCov">          1 :       lua_rawget(L, lua_upvalueindex(1));</span>
<span class="lineNum">     436 </span><span class="lineCov">          1 :       validproxy = lua_toboolean(L, -1);</span>
<span class="lineNum">     437 </span><span class="lineCov">          1 :       lua_pop(L, 1);  /* remove value */</span>
<span class="lineNum">     438 </span>            :     }
<span class="lineNum">     439 </span><span class="lineCov">          2 :     luaL_argcheck(L, validproxy, 1, &quot;boolean or proxy expected&quot;);</span>
<span class="lineNum">     440 </span><span class="lineCov">          1 :     lua_getmetatable(L, 1);  /* metatable is valid; get it */</span>
<span class="lineNum">     441 </span>            :   }
<span class="lineNum">     442 </span><span class="lineCov">          2 :   lua_setmetatable(L, 2);</span>
<span class="lineNum">     443 </span><span class="lineCov">          2 :   return 1;</span>
<span class="lineNum">     444 </span>            : }
<span class="lineNum">     445 </span>            : 
<span class="lineNum">     446 </span>            : 
<span class="lineNum">     447 </span>            : static const luaL_Reg base_funcs[] = {
<span class="lineNum">     448 </span>            :   {&quot;assert&quot;, luaB_assert},
<span class="lineNum">     449 </span>            :   {&quot;collectgarbage&quot;, luaB_collectgarbage},
<span class="lineNum">     450 </span>            :   {&quot;dofile&quot;, luaB_dofile},
<span class="lineNum">     451 </span>            :   {&quot;error&quot;, luaB_error},
<span class="lineNum">     452 </span>            :   {&quot;gcinfo&quot;, luaB_gcinfo},
<span class="lineNum">     453 </span>            :   {&quot;getfenv&quot;, luaB_getfenv},
<span class="lineNum">     454 </span>            :   {&quot;getmetatable&quot;, luaB_getmetatable},
<span class="lineNum">     455 </span>            :   {&quot;loadfile&quot;, luaB_loadfile},
<span class="lineNum">     456 </span>            :   {&quot;load&quot;, luaB_load},
<span class="lineNum">     457 </span>            :   {&quot;loadstring&quot;, luaB_loadstring},
<span class="lineNum">     458 </span>            :   {&quot;next&quot;, luaB_next},
<span class="lineNum">     459 </span>            :   {&quot;pcall&quot;, luaB_pcall},
<span class="lineNum">     460 </span>            :   {&quot;print&quot;, luaB_print},
<span class="lineNum">     461 </span>            :   {&quot;rawequal&quot;, luaB_rawequal},
<span class="lineNum">     462 </span>            :   {&quot;rawget&quot;, luaB_rawget},
<span class="lineNum">     463 </span>            :   {&quot;rawset&quot;, luaB_rawset},
<span class="lineNum">     464 </span>            :   {&quot;select&quot;, luaB_select},
<span class="lineNum">     465 </span>            :   {&quot;setfenv&quot;, luaB_setfenv},
<span class="lineNum">     466 </span>            :   {&quot;setmetatable&quot;, luaB_setmetatable},
<span class="lineNum">     467 </span>            :   {&quot;tonumber&quot;, luaB_tonumber},
<span class="lineNum">     468 </span>            :   {&quot;tostring&quot;, luaB_tostring},
<span class="lineNum">     469 </span>            :   {&quot;type&quot;, luaB_type},
<span class="lineNum">     470 </span>            :   {&quot;unpack&quot;, luaB_unpack},
<span class="lineNum">     471 </span>            :   {&quot;xpcall&quot;, luaB_xpcall},
<span class="lineNum">     472 </span>            :   {NULL, NULL}
<span class="lineNum">     473 </span>            : };
<span class="lineNum">     474 </span>            : 
<span class="lineNum">     475 </span>            : 
<span class="lineNum">     476 </span>            : /*
<span class="lineNum">     477 </span>            : ** {======================================================
<span class="lineNum">     478 </span>            : ** Coroutine library
<span class="lineNum">     479 </span>            : ** =======================================================
<span class="lineNum">     480 </span>            : */
<span class="lineNum">     481 </span>            : 
<span class="lineNum">     482 </span>            : #define CO_RUN  0       /* running */
<span class="lineNum">     483 </span>            : #define CO_SUS  1       /* suspended */
<span class="lineNum">     484 </span>            : #define CO_NOR  2       /* 'normal' (it resumed another coroutine) */
<span class="lineNum">     485 </span>            : #define CO_DEAD 3
<span class="lineNum">     486 </span>            : 
<span class="lineNum">     487 </span>            : static const char *const statnames[] =
<span class="lineNum">     488 </span>            :     {&quot;running&quot;, &quot;suspended&quot;, &quot;normal&quot;, &quot;dead&quot;};
<span class="lineNum">     489 </span>            : 
<span class="lineNum">     490 </span><span class="lineCov">       5967 : static int costatus (lua_State *L, lua_State *co) {</span>
<span class="lineNum">     491 </span><span class="lineCov">       5967 :   if (L == co) return CO_RUN;</span>
<span class="lineNum">     492 </span><span class="lineCov">       5967 :   switch (lua_status(co)) {</span>
<span class="lineNum">     493 </span><span class="lineCov">       5947 :     case LUA_YIELD:</span>
<span class="lineNum">     494 </span><span class="lineCov">       5947 :       return CO_SUS;</span>
<span class="lineNum">     495 </span><span class="lineCov">         20 :     case 0: {</span>
<span class="lineNum">     496 </span>            :       lua_Debug ar;
<span class="lineNum">     497 </span><span class="lineCov">         20 :       if (lua_getstack(co, 0, &amp;ar) &gt; 0)  /* does it have frames? */</span>
<span class="lineNum">     498 </span><span class="lineNoCov">          0 :         return CO_NOR;  /* it is running */</span>
<span class="lineNum">     499 </span><span class="lineCov">         20 :       else if (lua_gettop(co) == 0)</span>
<span class="lineNum">     500 </span><span class="lineCov">          3 :           return CO_DEAD;</span>
<span class="lineNum">     501 </span>            :       else
<span class="lineNum">     502 </span><span class="lineCov">         17 :         return CO_SUS;  /* initial state */</span>
<span class="lineNum">     503 </span>            :     }
<span class="lineNum">     504 </span><span class="lineNoCov">          0 :     default:  /* some error occured */</span>
<span class="lineNum">     505 </span><span class="lineNoCov">          0 :       return CO_DEAD;</span>
<span class="lineNum">     506 </span>            :   }
<span class="lineNum">     507 </span>            : }
<span class="lineNum">     508 </span>            : 
<span class="lineNum">     509 </span>            : 
<span class="lineNum">     510 </span><span class="lineCov">          4 : static int luaB_costatus (lua_State *L) {</span>
<span class="lineNum">     511 </span><span class="lineCov">          4 :   lua_State *co = lua_tothread(L, 1);</span>
<span class="lineNum">     512 </span><span class="lineCov">          4 :   luaL_argcheck(L, co, 1, &quot;coroutine expected&quot;);</span>
<span class="lineNum">     513 </span><span class="lineCov">          3 :   lua_pushstring(L, statnames[costatus(L, co)]);</span>
<span class="lineNum">     514 </span><span class="lineCov">          3 :   return 1;</span>
<span class="lineNum">     515 </span>            : }
<span class="lineNum">     516 </span>            : 
<span class="lineNum">     517 </span>            : 
<span class="lineNum">     518 </span><span class="lineCov">       5964 : static int auxresume (lua_State *L, lua_State *co, int narg) {</span>
<span class="lineNum">     519 </span><span class="lineCov">       5964 :   int status = costatus(L, co);</span>
<span class="lineNum">     520 </span><span class="lineCov">       5964 :   if (!lua_checkstack(co, narg))</span>
<span class="lineNum">     521 </span><span class="lineNoCov">          0 :     luaL_error(L, &quot;too many arguments to resume&quot;);</span>
<span class="lineNum">     522 </span><span class="lineCov">       5964 :   if (status != CO_SUS) {</span>
<span class="lineNum">     523 </span><span class="lineCov">          2 :     lua_pushfstring(L, &quot;cannot resume %s coroutine&quot;, statnames[status]);</span>
<span class="lineNum">     524 </span><span class="lineCov">          2 :     return -1;  /* error flag */</span>
<span class="lineNum">     525 </span>            :   }
<span class="lineNum">     526 </span><span class="lineCov">       5962 :   lua_xmove(L, co, narg);</span>
<span class="lineNum">     527 </span><span class="lineCov">       5962 :   lua_setlevel(L, co);</span>
<span class="lineNum">     528 </span><span class="lineCov">       5962 :   status = lua_resume(co, narg);</span>
<span class="lineNum">     529 </span><span class="lineCov">       5962 :   if (status == 0 || status == LUA_YIELD) {</span>
<span class="lineNum">     530 </span><span class="lineCov">       5961 :     int nres = lua_gettop(co);</span>
<span class="lineNum">     531 </span><span class="lineCov">       5961 :     if (!lua_checkstack(L, nres + 1))</span>
<span class="lineNum">     532 </span><span class="lineNoCov">          0 :       luaL_error(L, &quot;too many results to resume&quot;);</span>
<span class="lineNum">     533 </span><span class="lineCov">       5961 :     lua_xmove(co, L, nres);  /* move yielded values */</span>
<span class="lineNum">     534 </span><span class="lineCov">       5961 :     return nres;</span>
<span class="lineNum">     535 </span>            :   }
<span class="lineNum">     536 </span>            :   else {
<span class="lineNum">     537 </span><span class="lineCov">          1 :     lua_xmove(co, L, 1);  /* move error message */</span>
<span class="lineNum">     538 </span><span class="lineCov">          1 :     return -1;  /* error flag */</span>
<span class="lineNum">     539 </span>            :   }
<span class="lineNum">     540 </span>            : }
<span class="lineNum">     541 </span>            : 
<span class="lineNum">     542 </span>            : 
<span class="lineNum">     543 </span><span class="lineCov">       5948 : static int luaB_coresume (lua_State *L) {</span>
<span class="lineNum">     544 </span><span class="lineCov">       5948 :   lua_State *co = lua_tothread(L, 1);</span>
<span class="lineNum">     545 </span>            :   int r;
<span class="lineNum">     546 </span><span class="lineCov">       5948 :   luaL_argcheck(L, co, 1, &quot;coroutine expected&quot;);</span>
<span class="lineNum">     547 </span><span class="lineCov">       5947 :   r = auxresume(L, co, lua_gettop(L) - 1);</span>
<span class="lineNum">     548 </span><span class="lineCov">       5947 :   if (r &lt; 0) {</span>
<span class="lineNum">     549 </span><span class="lineCov">          3 :     lua_pushboolean(L, 0);</span>
<span class="lineNum">     550 </span><span class="lineCov">          3 :     lua_insert(L, -2);</span>
<span class="lineNum">     551 </span><span class="lineCov">          3 :     return 2;  /* return false + error message */</span>
<span class="lineNum">     552 </span>            :   }
<span class="lineNum">     553 </span>            :   else {
<span class="lineNum">     554 </span><span class="lineCov">       5944 :     lua_pushboolean(L, 1);</span>
<span class="lineNum">     555 </span><span class="lineCov">       5944 :     lua_insert(L, -(r + 1));</span>
<span class="lineNum">     556 </span><span class="lineCov">       5944 :     return r + 1;  /* return true + `resume' returns */</span>
<span class="lineNum">     557 </span>            :   }
<span class="lineNum">     558 </span>            : }
<span class="lineNum">     559 </span>            : 
<span class="lineNum">     560 </span>            : 
<span class="lineNum">     561 </span><span class="lineCov">         17 : static int luaB_auxwrap (lua_State *L) {</span>
<span class="lineNum">     562 </span><span class="lineCov">         17 :   lua_State *co = lua_tothread(L, lua_upvalueindex(1));</span>
<span class="lineNum">     563 </span><span class="lineCov">         17 :   int r = auxresume(L, co, lua_gettop(L));</span>
<span class="lineNum">     564 </span><span class="lineCov">         17 :   if (r &lt; 0) {</span>
<span class="lineNum">     565 </span><span class="lineNoCov">          0 :     if (lua_isstring(L, -1)) {  /* error object is a string? */</span>
<span class="lineNum">     566 </span><span class="lineNoCov">          0 :       luaL_where(L, 1);  /* add extra info */</span>
<span class="lineNum">     567 </span><span class="lineNoCov">          0 :       lua_insert(L, -2);</span>
<span class="lineNum">     568 </span><span class="lineNoCov">          0 :       lua_concat(L, 2);</span>
<span class="lineNum">     569 </span>            :     }
<span class="lineNum">     570 </span><span class="lineNoCov">          0 :     lua_error(L);  /* propagate error */</span>
<span class="lineNum">     571 </span>            :   }
<span class="lineNum">     572 </span><span class="lineCov">         17 :   return r;</span>
<span class="lineNum">     573 </span>            : }
<span class="lineNum">     574 </span>            : 
<span class="lineNum">     575 </span>            : 
<span class="lineNum">     576 </span><span class="lineCov">         23 : static int luaB_cocreate (lua_State *L) {</span>
<span class="lineNum">     577 </span><span class="lineCov">         23 :   lua_State *NL = lua_newthread(L);</span>
<span class="lineNum">     578 </span><span class="lineCov">         23 :   luaL_argcheck(L, lua_isfunction(L, 1) &amp;&amp; !lua_iscfunction(L, 1), 1,</span>
<span class="lineNum">     579 </span>            :     &quot;Lua function expected&quot;);
<span class="lineNum">     580 </span><span class="lineCov">         22 :   lua_pushvalue(L, 1);  /* move function to top */</span>
<span class="lineNum">     581 </span><span class="lineCov">         22 :   lua_xmove(L, NL, 1);  /* move function from L to NL */</span>
<span class="lineNum">     582 </span><span class="lineCov">         22 :   return 1;</span>
<span class="lineNum">     583 </span>            : }
<span class="lineNum">     584 </span>            : 
<span class="lineNum">     585 </span>            : 
<span class="lineNum">     586 </span><span class="lineCov">          2 : static int luaB_cowrap (lua_State *L) {</span>
<span class="lineNum">     587 </span><span class="lineCov">          2 :   luaB_cocreate(L);</span>
<span class="lineNum">     588 </span><span class="lineCov">          2 :   lua_pushcclosure(L, luaB_auxwrap, 1);</span>
<span class="lineNum">     589 </span><span class="lineCov">          2 :   return 1;</span>
<span class="lineNum">     590 </span>            : }
<span class="lineNum">     591 </span>            : 
<span class="lineNum">     592 </span>            : 
<span class="lineNum">     593 </span><span class="lineCov">       5949 : static int luaB_yield (lua_State *L) {</span>
<span class="lineNum">     594 </span><span class="lineCov">       5949 :   return lua_yield(L, lua_gettop(L));</span>
<span class="lineNum">     595 </span>            : }
<span class="lineNum">     596 </span>            : 
<span class="lineNum">     597 </span>            : 
<span class="lineNum">     598 </span><span class="lineCov">          1 : static int luaB_corunning (lua_State *L) {</span>
<span class="lineNum">     599 </span><span class="lineCov">          1 :   if (lua_pushthread(L))</span>
<span class="lineNum">     600 </span><span class="lineCov">          1 :     lua_pushnil(L);  /* main thread is not a coroutine */</span>
<span class="lineNum">     601 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">     602 </span>            : }
<span class="lineNum">     603 </span>            : 
<span class="lineNum">     604 </span>            : 
<span class="lineNum">     605 </span>            : static const luaL_Reg co_funcs[] = {
<span class="lineNum">     606 </span>            :   {&quot;create&quot;, luaB_cocreate},
<span class="lineNum">     607 </span>            :   {&quot;resume&quot;, luaB_coresume},
<span class="lineNum">     608 </span>            :   {&quot;running&quot;, luaB_corunning},
<span class="lineNum">     609 </span>            :   {&quot;status&quot;, luaB_costatus},
<span class="lineNum">     610 </span>            :   {&quot;wrap&quot;, luaB_cowrap},
<span class="lineNum">     611 </span>            :   {&quot;yield&quot;, luaB_yield},
<span class="lineNum">     612 </span>            :   {NULL, NULL}
<span class="lineNum">     613 </span>            : };
<span class="lineNum">     614 </span>            : 
<span class="lineNum">     615 </span>            : /* }====================================================== */
<span class="lineNum">     616 </span>            : 
<span class="lineNum">     617 </span>            : 
<span class="lineNum">     618 </span><span class="lineCov">        174 : static void auxopen (lua_State *L, const char *name,</span>
<span class="lineNum">     619 </span>            :                      lua_CFunction f, lua_CFunction u) {
<span class="lineNum">     620 </span><span class="lineCov">        174 :   lua_pushcfunction(L, u);</span>
<span class="lineNum">     621 </span><span class="lineCov">        174 :   lua_pushcclosure(L, f, 1);</span>
<span class="lineNum">     622 </span><span class="lineCov">        174 :   lua_setfield(L, -2, name);</span>
<span class="lineNum">     623 </span><span class="lineCov">        174 : }</span>
<span class="lineNum">     624 </span>            : 
<span class="lineNum">     625 </span>            : 
<span class="lineNum">     626 </span><span class="lineCov">         87 : static void base_open (lua_State *L) {</span>
<span class="lineNum">     627 </span>            :   /* set global _G */
<span class="lineNum">     628 </span><span class="lineCov">         87 :   lua_pushvalue(L, LUA_GLOBALSINDEX);</span>
<span class="lineNum">     629 </span><span class="lineCov">         87 :   lua_setglobal(L, &quot;_G&quot;);</span>
<span class="lineNum">     630 </span>            :   /* open lib into global table */
<span class="lineNum">     631 </span><span class="lineCov">         87 :   luaL_register(L, &quot;_G&quot;, base_funcs);</span>
<span class="lineNum">     632 </span><span class="lineCov">         87 :   lua_pushliteral(L, LUA_VERSION);</span>
<span class="lineNum">     633 </span><span class="lineCov">         87 :   lua_setglobal(L, &quot;_VERSION&quot;);  /* set global _VERSION */</span>
<span class="lineNum">     634 </span>            :   /* `ipairs' and `pairs' need auxiliary functions as upvalues */
<span class="lineNum">     635 </span><span class="lineCov">         87 :   auxopen(L, &quot;ipairs&quot;, luaB_ipairs, ipairsaux);</span>
<span class="lineNum">     636 </span><span class="lineCov">         87 :   auxopen(L, &quot;pairs&quot;, luaB_pairs, luaB_next);</span>
<span class="lineNum">     637 </span>            :   /* `newproxy' needs a weaktable as upvalue */
<span class="lineNum">     638 </span><span class="lineCov">         87 :   lua_createtable(L, 0, 1);  /* new table `w' */</span>
<span class="lineNum">     639 </span><span class="lineCov">         87 :   lua_pushvalue(L, -1);  /* `w' will be its own metatable */</span>
<span class="lineNum">     640 </span><span class="lineCov">         87 :   lua_setmetatable(L, -2);</span>
<span class="lineNum">     641 </span><span class="lineCov">         87 :   lua_pushliteral(L, &quot;kv&quot;);</span>
<span class="lineNum">     642 </span><span class="lineCov">         87 :   lua_setfield(L, -2, &quot;__mode&quot;);  /* metatable(w).__mode = &quot;kv&quot; */</span>
<span class="lineNum">     643 </span><span class="lineCov">         87 :   lua_pushcclosure(L, luaB_newproxy, 1);</span>
<span class="lineNum">     644 </span><span class="lineCov">         87 :   lua_setglobal(L, &quot;newproxy&quot;);  /* set global `newproxy' */</span>
<span class="lineNum">     645 </span><span class="lineCov">         87 : }</span>
<span class="lineNum">     646 </span>            : 
<span class="lineNum">     647 </span>            : 
<span class="lineNum">     648 </span><span class="lineCov">         87 : LUALIB_API int luaopen_base (lua_State *L) {</span>
<span class="lineNum">     649 </span><span class="lineCov">         87 :   base_open(L);</span>
<span class="lineNum">     650 </span><span class="lineCov">         87 :   luaL_register(L, LUA_COLIBNAME, co_funcs);</span>
<span class="lineNum">     651 </span><span class="lineCov">         87 :   return 2;</span>
<span class="lineNum">     652 </span>            : }
<span class="lineNum">     653 </span>            : 
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.13</a></td></tr>
  </table>
  <br>

</body>
</html>
